package org.example.bussiness;/**
 * @author HeShengjin 2356899074@qq.com
 * @Description TODO
 * @createTime 2023年02月15日 15:56:00
 */

import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.UserTask;
import org.flowable.common.engine.impl.el.ExpressionManager;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.impl.bpmn.behavior.UserTaskActivityBehavior;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.task.service.TaskService;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hsj
 * @description:UserTask动态指定审批人员
 * @date 2023/2/15 15:56
 */
public class WfUserTaskActivityBehavior extends UserTaskActivityBehavior {

    public WfUserTaskActivityBehavior(UserTask userTask) {
        super(userTask);
    }

    @Override
    protected void handleAssignments(TaskService taskService, String assignee, String owner, List<String> candidateUsers, List<String> candidateGroups, TaskEntity task, ExpressionManager expressionManager, DelegateExecution execution, ProcessEngineConfigurationImpl processEngineConfiguration) {
        // 兼容之前XML的配置（已经指定...）
        if (StringUtils.isNotBlank(assignee) || candidateUsers.size() > 0 || candidateGroups.size() > 0) {
            super.handleAssignments(taskService, assignee, owner, candidateUsers, candidateGroups, task, expressionManager, execution, processEngineConfiguration);
            return;
        }
        //扩展定制的UserTask节点
        IWfProcessService wfProcessService = SpringUtil.getBean(IWfProcessService.class);
        WfTaskUser taskUser = wfProcessService.getTaskUser(task.getProcessDefinitionId(), task.getProcessInstanceId(), task.getTaskDefinitionKey());

        candidateUsers = new ArrayList<>(taskUser.getCandidateUserIds());
        candidateGroups = new ArrayList<>(taskUser.getCandidateGroupIds());
        assignee = taskUser.getAssignee();

        super.handleAssignments(taskService, assignee, owner, candidateUsers, candidateGroups, task, expressionManager, execution, processEngineConfiguration);
    }
}
